package cn.bigfire.compiler.mytest.parse01;

import cn.bigfire.compiler.mytest.lexer01.Token;

import java.util.ArrayList;
import java.util.List;

/**
 * token 读取器
 */
public class TokenReader {

    private List<Token> tokens = new ArrayList<>();
    private int index = 0;

    //构造 TokenReader-----------------------------------------------
    public TokenReader(){}

    public TokenReader(List<Token> tokens){
        this.tokens = tokens;
    }

    public static TokenReader of(List<Token> tokens) {
        return new TokenReader(tokens);
    }
    //工具方法-----------------------------------------------

    public Token read() {
        if (index < tokens.size()) {
            return tokens.get(index++);
        }
        return null;
    }

    public Token peek() {
        if (index < tokens.size()) {
            return tokens.get(index);
        }
        return null;
    }

    public void unread() {
        if (index > 0) {
            index--;
        }
    }







    //get set-----------------------------------------------
    public List<Token> getTokens() {
        return tokens;
    }

    public void setTokens(List<Token> tokens) {
        this.tokens = tokens;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

}
